import pymysql
from common.readaccountinfo import ReadConfig


def read_sql(sql_sentence):
    # 连接数据库
    my_connect = pymysql.connect(host=ReadConfig().get_mysql('host'),
                                 port=int(ReadConfig().get_mysql('port')),
                                 user=ReadConfig().get_mysql('user'),
                                 password=ReadConfig().get_mysql('password'),
                                 database=ReadConfig().get_mysql('database'),
                                 charset=ReadConfig().get_mysql('charset'))

    # 建立游标
    data = my_connect.cursor()
    # 执行语句
    num = data.execute(sql_sentence)
    # 提交游标数据
    my_connect.commit()
    # 判断查询结果数量
    if num > 1:  # 如果查询结果大于1,返回结果转换为列表套列表
        result = data.fetchall()
        X = []
        for m in result:
            x = []
            for n in m:
                x.append(n)
            X.append(x)
        return X
    else:  # 查询结果只有一条
        for i in data.fetchall():
            for j in i:
                if len(i) > 1:  # 如果该元组元素数量大于1返回整个元组
                    return i
                else:  # 如果该元组元素数量等于1返回该元素
                    return j


if __name__ == '__main__':
    sql1 = 'select * from user '
    result1 = read_sql(sql1)
    print(result1)